package com.llyb.tingshu.album.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llyb.tingshu.album.service.TrackInfoService;
import com.llyb.tingshu.album.service.VodService;
import com.llyb.tingshu.common.login.TsLogin;
import com.llyb.tingshu.common.result.Result;
import com.llyb.tingshu.common.util.AuthContextHolder;
import com.llyb.tingshu.model.album.TrackInfo;
import com.llyb.tingshu.query.album.TrackInfoQuery;
import com.llyb.tingshu.vo.album.AlbumTrackListVo;
import com.llyb.tingshu.vo.album.TrackInfoVo;
import com.llyb.tingshu.vo.album.TrackListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;



	/**
	 * 批量获取下单付费声音列表
	 * @param trackId
	 * @param trackCount
	 * @return
	 */
	@GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
	Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount){

		List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(trackId, trackCount);
		return Result.ok(list);
	}


	@TsLogin
	@Operation(summary = "查找用户需要购买的声音列表")
	@GetMapping("/findUserTrackPaidList/{trackId}")
	public Result findUserTrackPaidList(@PathVariable("trackId") Long trackId){
		Long userId = AuthContextHolder.getUserId();

		List<Map<String,Object>> list = trackInfoService.findUserTrackPaidList(trackId, userId);

		return Result.ok(list);

	}


	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result<Map<String,Object>> uploadTrack(@RequestBody MultipartFile file) {

		Map<String, Object> map = vodService.uploadTrack(file);

		return Result.ok(map);
	}

	@TsLogin
	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result<String> saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {

		Long userId = null == AuthContextHolder.getUserId() ? 1L : AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(trackInfoVo,userId);

		return Result.ok("保存成功");
	}


	@TsLogin
	@Operation(summary = "分页查询所有的声音")
	@PostMapping("/findUserTrackPage/{pageNo}/{pagesize}")
	public Result findUserTrackPage(@PathVariable("pageNo") Long pageNo,
									@PathVariable("pagesize") Long pagesize,
									@RequestBody TrackInfoQuery trackInfoQuery
	) {

		Page<TrackListVo> page = new Page<>(pageNo, pagesize);
		Long userId = null == AuthContextHolder.getUserId() ? 1L : AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);

		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(page,trackInfoQuery);

		return Result.ok(trackListVoIPage);
	}

	@TsLogin
	@Operation(summary = "根据id删除声音")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable("trackId") Long trackId) {

		boolean result = trackInfoService.removeTrackInfo(trackId);

		return result ? Result.ok("删除成功") : Result.fail("删除失败");
	}


	@Operation(summary = "根据id查询声音并回显")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result<TrackInfo> getTrackInfo(@PathVariable("trackId") Long trackId) {

		TrackInfo trackInfo = trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}


	@TsLogin
	@Operation(summary = "更新声音")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@RequestBody TrackInfo trackInfo) {

		boolean result = trackInfoService.updateTrackInfo(trackInfo);

		return result ? Result.ok("更新成功") : Result.fail("更新失败");
	}


	@TsLogin(required = false)
	@Operation(summary = "根据专辑id获取声音列表")
	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
	public Result findAlbumTrackPage(@PathVariable("albumId") Long albumId,
									 @PathVariable("pageNo") Long pageNo,
									 @PathVariable("pageSize") Long pageSize){

		Long userId = AuthContextHolder.getUserId();
		IPage<AlbumTrackListVo> iPage = new Page<>(pageNo, pageSize);
		IPage<AlbumTrackListVo> trackListVoIPage = trackInfoService.findAlbumTrackPage(iPage, albumId,userId);

		return Result.ok(trackListVoIPage);
	}
}

